<%@ page contentType="text/html;charset=UTF-8" trimDirectiveWhitespaces="true"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<c:set var="extLibs" value="validate,ztree"/>
<sys:header title="角色管理 - ${role.isNewRecord ? '新增' : '编辑'}角色" extLibs="${extLibs}"/>
<div class="portlet box ${boxStyleColor} autoHeight">
	<div class="portlet-title">
		<div class="caption"><i class="fa icon-users"></i>${role.isNewRecord ? '新增' : type eq '2' ? '授权' : '编辑'}角色</div>
	</div>
	<div class="portlet-body form">
		<form:form id="inputForm" modelAttribute="role" action="${ctx}/sys/role/save" method="post" class="form-horizontal">
			<input type="hidden" name="type" value="${type }" />
			<sys:message content="${message}"/>
			<div class="form-body">
				<h4 class="form-section ">基本信息</h4>
				<c:choose><c:when test="${role.currentUser.admin}">
					<div class="row">
						<div class="col-xs-6">
							<div class="form-group">
								<label class="control-label col-sm-4"><span class="required">*</span> 角色编码：</label>
								<div class="col-sm-8">
									<form:hidden path="isNewRecord"/>
									<form:input path="roleCode" htmlEscape="false" maxlength="64" readonly="${!role.isNewRecord}" class="form-control abc required"/>
									<span class="help-block">&nbsp;</span>
								</div>
							</div>
						</div>
					</div>
				</c:when><c:otherwise>
					<form:hidden path="roleCode"/>
				</c:otherwise></c:choose>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4"><span class="required">*</span> 角色名称：</label>
							<div class="col-sm-8">
								<input id="oldName" name="oldName" type="hidden" value="${role.roleName}">
								<form:input path="roleName" htmlEscape="false" maxlength="50" class="form-control required"/>
							</div>
						</div>
					</div>
				</div>
				<%-- <div class="row basic_msg">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4"><span class="required">*</span> 英文名称：</label>
							<div class="col-sm-8">
								<input id="oldEnname" name="oldEnname" type="hidden" value="${role.nameEn}">
								<form:input path="nameEn" htmlEscape="false" maxlength="50" class="form-control required"/>
							</div>
						</div>
					</div>
				</div> --%>
				<div class="row basic_msg">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2">备注信息：</label>
							<div class="col-sm-8">
								<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<h4 class="form-section extend_msg">角色授权</h4>
				<div class="row extend_msg">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4">功能权限：</label>
							<div class="col-sm-8">
								<div>
									<a href="javascript:" onclick="tree.expandAll(true);">全部展开</a>&nbsp;
									<a href="javascript:" onclick="tree.expandAll(false);">全部折叠</a>
								</div>
								<div id="menuTree" class="ztree" style="margin-top:3px;float:left;"></div>
								<form:hidden path="menuIds"/>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="form-actions fluid">
				<div class="row">
					<div class="col-xs-12">
						<div class="col-sm-offset-2 col-sm-10">
							<shiro:hasPermission name="sys:role:edit">
								<button id="btnSubmit" type="submit" class="btn btn-primary"><i class="fa fa-check"></i> 保 存</button>&nbsp;
							</shiro:hasPermission>
							<button id="btnCancel" type="button" class="btn default" onclick="closeCurrentTabPage('${param.parentTabPageId}')"><i class="fa fa-reply-all"></i> 关 闭</button>
						</div>
					</div>
				</div>
			</div>
		</form:form>
	</div>
</div>
<sys:footer extLibs="${extLibs}"/>
<script type="text/javascript">
	var tree;
	$(document).ready(function() {
		$("#inputForm").validate({
			rules: {
				roleName: {remote: "${ctx}/sys/role/checkName?oldName=" + encodeURIComponent("${role.roleName}")}/* ,
				nameEn: {remote: "${ctx}/sys/role/checkEnname?oldEnname=" + encodeURIComponent("${role.nameEn}")} */
			},
			messages: {
				roleName: {remote: "角色名已存在"}/* ,
				nameEn: {remote: "英文名已存在"} */
			},
			submitHandler: function(form){
				if("${type}" == "0" || "${type}" == "2"){
					var ids = [], nodes = tree.getCheckedNodes(true);
					for(var i=0; i<nodes.length; i++) {
						ids.push(nodes[i].id);
					}
					$("#menuIds").val(ids);
				}
				ajaxSubmitForm($(form), function(data){
					if(data.result == Global.TRUE){
						closeCurrentTabPage('${param.parentTabPageId}', function(contents, contentWindow){
							contentWindow.page();
						});
					}
					showMessage(data.message);
				}, "json");
		    }
		});
		
		//  ======== 加载授权树结构  ========
		if("${type}" == "0" || "${type}" == "2"){
			var setting = {check:{enable:true,nocheckInherit:true},
					view:{selectedMulti:false,nameIsHTML: true},
					data:{simpleData:{enable:true},key:{title:"title"}},
					callback:{
						beforeClick:function(id, node){
							tree.checkNode(node, !node.checked, true, true);
							return false;
						}
					}};
			
			// 用户-菜单
			var zNodes=[
					<c:forEach items="${menuList}" var="menu">{id:"${menu.id}", pId:"${not empty menu.parent.id ? menu.parent.id : 0}", name:"${not empty menu.parent.id ? menu.name : '权限列表'}${'<font color=#888> &nbsp; &nbsp; '}${menu.permission}${' &nbsp; '}${menu.href}${'</font>'}", title:"${menu.name}&nbsp;${menu.permission}\n${menu.href}"},
		            </c:forEach>];
			// 初始化树结构
			tree = $.fn.zTree.init($("#menuTree"), setting, zNodes);
			// 不选择父节点
			tree.setting.check.chkboxType = { "Y" : "ps", "N" : "ps" };
			// 默认选择节点
			var ids = "${role.menuIds}".split(",");
			for(var i=0; i<ids.length; i++) {
				var node = tree.getNodeByParam("id", ids[i]);
				try{tree.checkNode(node, true, false);}catch(e){}
			}
			// 默认展开一级节点
			var nodes = tree.getNodesByParam("level", 0);
			for(var i=0; i<nodes.length; i++) {
				tree.expandNode(nodes[i], true, false, false);
			}
		}

		if("${type}" == "1"){
			jQuery('#inputForm').action='${ctx}/sys/role/saveGrant';
			jQuery('#inputForm .extend_msg').hide();
		}else if("${type}" == "2"){
			jQuery('#inputForm').action='${ctx}/sys/role/saveBasic';
			jQuery('#inputForm .basic_msg').hide();
			jQuery('#oldName').parent().parent().find('span.required').css('display','none');
			jQuery('#roleName').attr("disabled","disabled");
		}
	});
</script>